import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:saas_order_meal/net/net_response/menu/regular_menu_data.dart';

import '../res/resources.dart';

typedef ItemBuilder<T> = Widget Function(BuildContext context,T item,int index,bool isSelected);
typedef CanSelected<T> = String Function(T item);
class SelectItemWidget<T> extends StatelessWidget {
  var position = 0;
  final List<T> items;
  final ItemBuilder<T> itemBuilder;
  final CanSelected<T>? canSelected;
  IndexedWidgetBuilder? separatorBuilder;
  Axis scrollDirection;
  bool shrinkWrap;
  EdgeInsets padding;
  final ScrollController? controller;

  Function(int index, T item)? onCallback;



  SelectItemWidget(this.items,
      {required this.itemBuilder,this.position = 0,
        this.onCallback,
        this.separatorBuilder,
        this.scrollDirection = Axis.vertical,
        this.canSelected,
        this.shrinkWrap = false,this.padding = EdgeInsets.zero,this.controller});

  @override
  Widget build(BuildContext context) {
    return StatefulBuilder(builder: (context, setState) {
      return ListView.separated(
        controller: controller,
        itemBuilder: (context, index) {
          return itemBuilder(context,items[index],index,position==index).onTap(() {
            var str = "";
            if(canSelected!=null){
              str = canSelected!(items[index]);
            }
            if(!str.isNullOrEmpty()){
              ToastUtils.shotToast(str, context: context);
              return;
            }
            setState(() {
              position = index;
            });
            if (onCallback != null) {
              onCallback!(index, items[index]);
            }
          },highlightColor: Colors.transparent,splashColor: Colors.transparent);
        },
        separatorBuilder: (context, index) {
          return separatorBuilder == null
              ? Container()
              : separatorBuilder!(context, index);
        },
        itemCount: items.length,
        scrollDirection: scrollDirection,
        shrinkWrap: shrinkWrap,
        padding: padding,
      );
    });
  }
}

Widget titleImage(String type, bool selected) {
  if (type == "1") {
    return Images.breakfastImage(selected);
  }

  if (type == "2") {
    return Images.lunchImage(selected);
  }

  if (type == "3") {
    return Images.dinnerImage(selected);
  }

  if (type == "4") {
    return Images.breakfastImage(selected);
  }

  if (type == "5") {
    return Images.lunchImage(selected);
  }

  if (type == "6") {
    return Images.dinnerImage(selected);
  }

  return Container();
}


Widget LeftTimeTypeItemWidget(RegularMenuData e, bool selected) {
  num count = 0;
  for(var item in e.timeSectionList??<RegularMenuTimeSectionList>[]){
    for(var food in item.selectList){
      count+=food.number;
    }
  }
  return Stack(
    children: [
      Column(
        children: [
          titleImage(e.timeSection.toString(), selected),
          Container(
            height: 8,
          ),
          CommonText(e.timeSectionName, textColor: Colors.grey[700]!)
        ],
      ),
      if(count>0)
      CommonText("${count}",textSize: 14,textColor: Colors.white).addToContainer(
        decoration: BoxDecoration(
          color: Colors.red,borderRadius: 22.borderRadius
        ),
        alignment: Alignment.center,
        constraints: BoxConstraints(minWidth: 25,minHeight: 25)
      ).setLocation(right: 0)
    ],
  ).addToContainer(margin: EdgeInsets.only(bottom: 10));
}
